/**
 * ThirdPartyResponseStringValidator.java
 * author: yujiakui
 * 2017年8月29日
 * 上午10:22:01
 */
package com.ctim.service.integration.validator.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.ctim.common.tool.compress.CompressStringUtil;
import com.ctim.common.tool.encrypt.Base64;
import com.ctim.service.enums.ValidatorTypeEnum;
import com.ctim.service.integration.validator.ThirdPartyResponseValidator;
import com.ctim.service.integration.validator.ThirdPartyValidatorConfig;
import com.ctim.service.integration.validator.ThirdPartyValidatorResult;
import com.google.common.collect.Lists;

/**
 * @author yujiakui
 *
 *         上午10:22:01
 *
 *         第三方接口返回是String的时候校验
 */
@Component("thirdPartyResponseStringValidator")
public class ThirdPartyResponseStringValidator implements ThirdPartyResponseValidator {

	/** 状态 */
	private static final String STATUS = "status";

	/** 错误码 */
	private static final String ERROR_CODE = "errorCode";

	/** 错误信息 */
	private static final String ERROR_MESSAGE = "errorMessage";

	/** 解析对象对应的内容，即是对应的返回值 */
	private static final String RETURN_VALUE = "returnValue";

	/** 成功状态值 */
	private static final String SUCCESS_STATUS = "1";

	/*
	 *
	 * @see com.ctim.service.integration.validator.ThirdPartyResponseValidator#
	 * validate(java.lang.Object,
	 * com.ctim.service.integration.validator.ThirdPartyValidatorConfig)
	 */
	@Override
	public ThirdPartyValidatorResult validate(Object inputObj,
			ThirdPartyValidatorConfig thirdPartyValidatorConfig) {
		ThirdPartyValidatorResult result = new ThirdPartyValidatorResult();

		if (!(inputObj instanceof String)) {
			result.setSuccess(false);
			result.setErrorReason(
					"校验器ThirdPartyResponseStringValidator对应校验的输入对象不是String类型,inputObj=" + inputObj);
			return result;
		}

		String inputStr = (String) inputObj;
		String status = extractTagText(inputStr, STATUS);
		if (!SUCCESS_STATUS.equals(status)) {
			// 说明失败
			result.setSuccess(false);
			result.setErrorCode(extractTagText(inputStr, ERROR_CODE));
			result.setErrorReason(extractTagText(inputStr, ERROR_MESSAGE));
			return result;
		}
		// 获取解析对象对应的内容
		String originResult = extractTagText(inputStr, RETURN_VALUE);
		// 解压
		String parserObjCnt = unzipResultUsingBase64(originResult);

		result.setParserObj(parserObjCnt);
		return result;
	}

	/**
	 * 对返回结果进行base64解压
	 *
	 * @param result
	 * @return
	 */
	private String unzipResultUsingBase64(String result) {
		// 返回字符串，解析处理字符串内容
		Base64 base64 = new Base64();
		byte[] bytes = base64.decode(result);
		return CompressStringUtil.decompress(bytes);

	}

	/**
	 * 抽取tag之间的内容
	 *
	 * @param inputStr
	 * @param tag
	 * @return
	 */
	private String extractTagText(String inputStr, String tag) {
		String startTag = "<" + tag + ">";
		String endTag = "</" + tag + ">";
		String[] startTagSplitInfos = inputStr.split(startTag);
		String[] endTagSplitInfos = startTagSplitInfos[1].split(endTag);

		return endTagSplitInfos[0];
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ctim.service.integration.validator.ThirdPartyResponseValidator#
	 * getValidatorTypes()
	 */
	@Override
	public List<ValidatorTypeEnum> getValidatorTypes() {
		return Lists.newArrayList(ValidatorTypeEnum.STRING_VALIDATOR);
	}

}
